package com.jfree.zk.curator;

import org.apache.curator.framework.CuratorFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author wangjian
 * @date 2019/7/16 17:25
 */
@Component
public class CuratorFrameworkUtil {
    public static Logger log = LoggerFactory.getLogger(CuratorFrameworkUtil.class);
    @Autowired
    private CuratorFramework client;

    /**
     * 获取节点数据
     * @param path
     * @return
     */
    public String getData(String path){
        try {
            return new String( client.getData().forPath(path));
        } catch (Exception e) {
            log.error("获取zk data错误，path={}",path);
            return null;
        }
    }

    /**
     * 更新节点数据
     * @param path
     * @param data
     */
    public void updateNodeDate( String path, String data) {
        try {
            client.setData().forPath(path, data.getBytes());
        } catch (Exception e) {
            System.out.println("更新节点数据失败, elog=" + e.getMessage());
        }
    }

    /**
     * 删除节点
     * @param path
     */
    public void deleteNode(String path) {
        try {
            client.delete().forPath(path);
        } catch (Exception e) {
            System.out.println("删除节点失败, elog=" + e.getMessage());
        }
    }
}
